//取消请求

let xhr = new XMLHttpRequest();
xhr.open("GET", "www.baidu.com");
xhr.send();
setTimeout(() => xhr.abort(), 300)



//axios取消请求
//方式一：
const CancelToken = axios.CancelToken;
const source = CancelToken.source();

axios.post('www.baidu.com', {
    name: 'wahaha'
}, {
    cancelToken: source.token
})
source.cancelToken('取消请求')

//方式二：
const CancelToken = axios.CancelToken;
let cancel;

axios.get('www.baidu.com', {
    cancelToken: new CancelToken(function executor(c) {
        cancel = c;
    })
})


//判断取消重复请求
import qs from 'qs';
const pendingRequest = new Map();

const requestKey = [method, url, qs.stringify(params), qs.stringify(data)].join('&');

const cancelToken = new CancelToken(function executor(cancel) {
    if (!pendingRequest.has(requestKey)) {
        pendingRequest.set(requestKey, cancel)
    }
})


//定义辅助函数
function generateReqKey(config) {
    const { method, url, params, data } = config;
    return [method, url, qs.stringify(params), qs.stringify(data)].join('&')
}

//addPendingRequest：用于把当前请求细腻些添加到pendingRequest对象
const pendingRequest = new Map();
function addPendingRequest(config) {
    const requestKey = generateReqKey(config);
    config.cancelToken = config.cancelToken || new axios.CancelToken((cancel) => {
        if (!pendingRequest.has(requestKey)) {
            pendingRequest.set(requestKey, cancel)
        }
    })
}

//removePendingRequest：检查是否存在重复请求,若存在则取消已发的请求
function removePendingRequest(config) {
    const requestKey = generateReqKey(config);
    if (pendingRequest.has(requestKey)) {
        const cancelToken = pendingRequest.get(requestKey);
        cancelToken(requestKey);
        pendingRequest.delete(requestKey)
    }
}

//设置请求拦截器
axios.interceptors.request.ues(
    function (config) {
        removePendingRequest(config); //检查是否存在重复请求，若存在则取消已发的请求
        addPendingRequest(config);    //把当前请求信息添加到pendingRequest对象中
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
)

//设置响应拦截器
axios.interceptors.response.ues(
    (response) => {
        removePendingRequest(response.config)
        return response;
    },
    (error) => {
        removePendingRequest(error.config || {});
        if (axios.isCancel(error)) {
            console.log("已取消的重复请求:" + error.message)
        } else {
            //添加异常处理
        }
        return Promise.reject(error);
    }
)


function createHttpService(settings) {
    const service = Axios.create(settings)
    service.interceptors.request.use(
        config => {
            const token = localStorage.getItem('access_token')
            config.headers.token = token
            return config
        },
        error => {
            return Promise.reject(error)
        }
    )

    service.interceptors.response.use(
        response => {
            console.log(response)
            const { code, message, data } = response.data

            if (code >= 30000) {
                console.log('>>>自定义错误信息，全局提示处理', message)
                return data
            }

            if (code >= 200 && code < 300) {
                return data
            }

            if (code >= 300 && code < 600) {
                return Promise.reject(response.data)
            }
        },
        error => {
            const { status = 404 } = error?.response
            if (Object.prototype.hasOwnProperty.call(codeMessage, status)) {
                haddleError(error, codeMessage[status])
            }
            throw error
        }
    )
    return service
}